package com.flexdms.i18n;

import java.sql.SQLException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Logger;

import javax.faces.application.Application;
import javax.faces.application.ApplicationWrapper;
import javax.faces.context.FacesContext;

import org.omnifaces.util.Faces;


public class LocalizedApplication extends ApplicationWrapper
{
	
	Logger logger=Logger.getLogger(LocalizedApplication.class.getName());
	Application parentApplication;

	public LocalizedApplication(Application parentApplication)
	{
		this.parentApplication = parentApplication;
	}
	
	public I18NMessage getI18nMessage()
	{
		return I18NMessage.getDatabaseI18nMessage();
	}
	
	
	@Override
	public void setSupportedLocales(Collection<Locale> locales)
	{
	
		I18NMessage i18nMessage=getI18nMessage();
		try
		{
			List<String> columns = i18nMessage.getColumns(i18nMessage.initTable());
			Iterator<Locale> lIterator=locales.iterator();
			while(lIterator.hasNext())
			{
				Locale locale=lIterator.next();
				String column=I18NMessage.localeToString(locale).toUpperCase();
				if (column.equals(i18nMessage.getDefaultLocale()))
				{
					continue;
				}
				if (!columns.contains(column))
				{
					i18nMessage.addColumn(column);
				}
			}
		} catch (SQLException e)
		{
			throw new RuntimeException(e);
		}
		
	}

	@Override
	public Application getWrapped()
	{
		return parentApplication;
	}
	
	@Override
	public Iterator<Locale> getSupportedLocales()
	{
		 return getI18nMessage().getSupportedLocales().iterator();
	}
	

	@Override
	public Locale getDefaultLocale()
	{
		return I18NMessage.stringToLocale(getI18nMessage().getDefaultLocale());
	}

	@Override
	public ResourceBundle getResourceBundle(FacesContext ctx, String name)
	{
		//"msgs" is the var for the message bundle
		if (name.equals(I18NMessage.getBasename()) || name.equals(I18NMessage.DEFAULT_BUNDLE_NAME))
		{
			I18NMessage i18nMessage=getI18nMessage();
			ResourceBundle rBundle= i18nMessage.loadBundle(i18nMessage.getCurrentLocale());
			return rBundle;
		}
		return super.getResourceBundle(ctx, name);
	}
	

	/*
	@Override
	public UIComponent createComponent(String componentType)
			throws FacesException
	{
		UIComponent component=super.createComponent(componentType);
		if (componentType.equals(HtmlOutputText.COMPONENT_TYPE) || componentType.equals(HtmlOutputLabel.COMPONENT_TYPE))
		{
			return LocalizationHelper.createOutput((UIOutput) component);
		} else {
			return component;
		}
		
		
	}

	@Override
	public UIComponent createComponent(FacesContext context,
			String componentType, String rendererType)
	{
		UIComponent component=super.createComponent(context, componentType, rendererType);
		if (componentType.equals(HtmlOutputText.COMPONENT_TYPE) || componentType.equals(HtmlOutputLabel.COMPONENT_TYPE))
		{
			return LocalizationHelper.createOutput((UIOutput) component);
		} else {
			return component;
		}
	}
	
	*/
	

	
}
